//
//  BKClassifyCommon.m
//  bestkeep
//
//  Created by 武探 on 2016/11/3.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKClassifyCommon.h"
#import "BKTreeDataModel.h"
#import "RequestManager.h"
#import "MJExtension.h"
#import "BKManyClassfiyKindsModel.h"
#import "BKScrollViewHeaderModel.h"
#import "BKUserStatusInfo.h"
#import "Mediator.h"
#import "Mediator+BKMy.h"
#import "BKIndicationView.h"

#if ApplicationEnvironmentProduction
NSString * const BKClassifyLocalizableTableName = @"BKClassifyLocalizable";
NSString * const BKClassifyStrAPI = @"http://api.bestkeep.cn/";
NSString * const BKClassify_goods_classify = @"getCategoryTreeList_v2";
#else
NSString * const BKClassifyLocalizableTableName = @"BKClassifyLocalizable";
NSString * const BKClassifyStrAPI = @"http://api.test.bestkeep.cn/";
NSString * const BKClassify_goods_classify = @"getCategoryTreeList_v2";
#endif


@implementation BKClassifyCommon

+(NSString *)localizedStringWithKey:(NSString *)key {
    return NSLocalizedStringFromTable(key, BKClassifyLocalizableTableName, nil);
}
//获取第一分类类名数组（头标题）
+(void)getTopClassWithCategoryType:(NSString *)categoryType categoryId:(NSString *)categoryId compeletion:(Compeletion)compeletion failure:(Failure)failure{
    
    NSString *url = [BKClassifyStrAPI stringByAppendingString:@"topOneCategoryListForApp"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:categoryType forKey:@"type"];
    [dict setValue:categoryId forKey:@"code"];
    [[RequestManager manager]bkDataRequest:RequestMethodPost URLString:url parameters:dict success:^(id  _Nullable responseObject) {
        NSLog(@"头标题%@",responseObject);
        
        if ([responseObject[@"success"] boolValue]) {
            
            NSDictionary *dataDict = [responseObject objectForKey:@"data"];
            BKScrollViewHeaderModel *headerModel = [BKScrollViewHeaderModel mj_objectWithKeyValues:dataDict];
            compeletion(headerModel,nil);
        }
      
    
       else
       {
           failure(responseObject,nil);
       }
        
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
          failure(responseObject,error);
    }];
}
// 分类树状数据
+(void)getGoodsClassifyTreeInfo:(UIView*)view compeletion:(Compeletion)compeletion failure:(Failure)failure
{
    
    NSString *urlstr = [BKClassifyStrAPI stringByAppendingString:BKClassify_goods_classify];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:urlstr parameters:nil success:^(id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
            BOOL success = [[responseObject objectForKey:@"success"] boolValue];
            if (success) {
                NSDictionary *data = [responseObject objectForKey:@"data"];
                BKTreeDataModel *treeData = [BKTreeDataModel mj_objectWithKeyValues:data];
                compeletion(treeData,nil);
            }else{
                 failure(responseObject,nil);
             }
        
      } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
          failure(responseObject,error);
    }];

  
}
//商品详情
+(void)getNewGoodsDetailClassifyTreeInfo:(NSDictionary *)goodsList compeletion:(Compeletion)compeletion failure:(Failure)failure{
    
    NSString *url = [BKClassifyStrAPI stringByAppendingString:@"searchGoodsList"];

    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:goodsList success:^(id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
        NSDictionary *result = responseObject;
        
        if ([responseObject[@"success"] boolValue]) {
            
            NSDictionary *dataDict = result[@"data"];
            BKManyClassfiyKindsModel *model = [BKManyClassfiyKindsModel mj_objectWithKeyValues:dataDict];
             compeletion(model,nil);
        }
        else
        {
            failure(responseObject,nil);
        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        failure(responseObject,error);
    }];
}

+(NSMutableAttributedString *)specialStringStyle:(NSString*)string fontOfSize:(CGFloat)fontOfSize{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:string];
    if ([string rangeOfString:@"起"].location != NSNotFound) {
        NSRange range = [string rangeOfString:@"起"];
        [str addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(178, 178, 178, 1) range:NSMakeRange(range.location, string.length - range.location)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontOfSize] range:NSMakeRange(range.location, string.length - range.location)];
    }
    return str;
}

+(BOOL)islogin{
    Mediator *mediator = [Mediator sharedInstance];
    NSDictionary *dic = [mediator BKMyUserInfo];
    NSNumber *result = dic[BKMyUserIsLoginKey];
    if ([result intValue] == 0) {
        return NO;
    }
    return YES;
    
}

+(void)haveNewInformation:(UIView*)badgeView{
    if ([BKClassifyCommon islogin]) {
        BKUserStatusInfo *userInfo = [BKUserStatusInfo currentUser];
        if (![userInfo.unreadMessageNum isEqualToString:@""]) {
            badgeView.hidden = NO;
        }else{
            badgeView.hidden = YES;
        }
    }else{
        badgeView.hidden = YES;
    }
}

@end
